import { Collapse, InfiniteScroll, NavBar, Switch } from "antd-mobile";
import axios from "../../utils/index";
import React, { useEffect, useState } from "react";
import { useSelector } from "react-redux";
import { Cell } from "react-vant";
import Biao from "../../components/index";
import { useNavigate } from "react-router-dom";
function Index() {
  const { start, end, names } = useSelector((state) => state.cont);
  const navigate = useNavigate();
  console.log(start, end, names);
  const [page, setpage] = useState(1);
  const [list, setList] = useState([]);
  const [isSeat, setisSeat] = useState(false);
  const [isSpeed, setisSpeed] = useState(false);
  const getData = async () => {
    const resp = await axios.post("/trip/train", {
      start,
      end,
      page,
      isSeat,
      isSpeed,
    });
    console.log(resp);
    setpage(page + 1);
    setList(list.concat(resp.data.data));
  };
  useEffect(() => {
    getData();
  }, []);

  return (
    <div>
      <NavBar>
        {start}--{end}
      </NavBar>
      <Cell.Group>
        <Collapse defaultActiveKey={["1"]}>
          {list.map((v, i) => {
            return (
              <Collapse.Panel
                key={i}
                title={
                  <div>
                    {v.start}---{v.end} :{v.startTime}
                  </div>
                }
              >
                <Biao
                  onClick={() => navigate("/xiang", { state: v })}
                  v={v}
                ></Biao>
              </Collapse.Panel>
            );
          })}
        </Collapse>
      </Cell.Group>
      <InfiniteScroll loadMore={getData} hasMore={true} />
      <div style={{ position: "fixed", bottom: "0" }}>
        <Switch
          onChange={() => {
            setisSeat(!isSeat), setList([]), setpage(1);
          }}
        />
        有座
        <Switch
          onChange={() => {
            setisSpeed(!isSpeed);
            setList([]);
            setpage(1);
          }}
        />
        高铁
      </div>
    </div>
  );
}

export default Index;
